﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using TNPTN.Constant;
using System.IO;

namespace TNPTN
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        #region Constants

        private const string FILE_COUNT_VISITOR = "Visitors";
        private const string SES_COUNT_BACKUP = "SES_BACKUP";
        #endregion
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            // Initialization of our Unity container
            Bootstrapper.Initialise();

            // Code that runs on application startup
            this.InitCountVisitor();
        }

        /// <summary>
        /// On Error
        /// </summary>
        protected void Application_Error(object sender, EventArgs e)
        {
            //Write log
            Exception ex = Server.GetLastError();
            TNPTN.Utility.Log.WriteLog(ex);
        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Session[Common.SES_COUNT_VISITOR] = (int) Application[Common.SES_COUNT_VISITOR] + 1;
            Application[Common.SES_COUNT_VISITOR] = Session[Common.SES_COUNT_VISITOR];
            
            this.BackupCountDate();
        }

        private void BackupCountDate()
        {
            DateTime temp = (DateTime)Application[SES_COUNT_BACKUP];
            if (DateTime.Now.Day != temp.Day)
            {
                using (StreamWriter outfile = new StreamWriter(FILE_COUNT_VISITOR,false))
                {
                    outfile.Write(((int)Application[Common.SES_COUNT_VISITOR]).ToString());
                }
                Application[SES_COUNT_BACKUP] = DateTime.Now;
            }
        }

        private void InitCountVisitor()
        {
            Application[Common.SES_COUNT_VISITOR] = ReadCountVisitor();
            Application[SES_COUNT_BACKUP] = DateTime.Now;
        }

        private int ReadCountVisitor()
        {
            int ret = 0;
            try
            {
                using (StreamReader sr = new StreamReader(FILE_COUNT_VISITOR))
                {
                    String line = sr.ReadToEnd();
                    if (!String.IsNullOrEmpty(line))
                    {
                        ret = Convert.ToInt32(line);
                    }
                }
            }
            catch
            {
            }
            return ret;
        }
    }
}